package com.youlin.controller;

import com.youlin.dto.*;
import com.youlin.dto.req.FindAllAdminReqDto;
import com.youlin.dto.res.AdminResDto;
import com.youlin.dto.req.EditAdminReqDto;
import com.youlin.service.BygAdminService;
import com.youlin.untils.AdminUtil;
import com.youlin.untils.MyBeanUtil;
import com.youlin.untils.MyEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiSort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 账号管理
 *
 * @author ${author}
 * @since 2020-06-01
 */
@ApiSort(4)
@Api(tags = {"4 - 账号管理"})
@RestController
@RequestMapping("/bygAdmin")
@CrossOrigin
public class BygAdminController {

    @Autowired
    private BygAdminService bygAdminService;

    /**
     * 获取全部账号信息列表
     *
     * @param baseReqDto
     * @return
     */
    @PostMapping("/findAllAdmins")
    @ApiOperationSort(value = 1)
    @ApiOperation(value = "1 - 获取全部账号信息列表")
    public PageDataResDto<List<AdminResDto>> findAllAdmins(@RequestBody PageDataReqDto<FindAllAdminReqDto> baseReqDto) {
        FindAllAdminReqDto data = baseReqDto.getData();
        PageInfo pageInfo = baseReqDto.getPageInfo();
        if (MyBeanUtil.isParamError(data)) return new PageDataResDto<>(Status.PARAMETERERROR);
        return bygAdminService.findAllAdmins(data.getUnitId(), data.getRoleId(), pageInfo);
    }

    /**
     * 新建/编辑单位登录账号信息
     *
     * @param baseReqDto
     * @return
     */
    @PostMapping("/editAdmin")
    @ApiOperationSort(value = 2)
    @ApiOperation(value = "2 - 新建/编辑单位登录账号信息",
            notes = "响应参数：status-message = {1008-登录账号重复，1009-账号使用者姓名重复}")
    public BaseResDto<Status, String> editAdmin(@RequestBody BaseReqDto<EditAdminReqDto> baseReqDto) {
        EditAdminReqDto data = baseReqDto.getData();
        if (data.getRoleId() == MyEnum.ROLE_REVIEWER_ID.getValue() && data.getPid() == null)
            data.setPid(AdminUtil.getCurrentAdmin().getId());
        if (MyBeanUtil.isParamError(data)) return new BaseResDto<>(Status.PARAMETERERROR);
        if (data.getId() == null) return bygAdminService.saveAdmin(data);
        else return bygAdminService.editAdmin(data);
    }

    /**
     * 校验当前账号是否有未完成的工作
     *
     * @param baseReqDto
     * @return
     */
    @PostMapping("/checkAdmin")
    @ApiOperationSort(value = 3)
    @ApiOperation(value = "3 - 校验当前账号是否有未完成的工作", notes = "请求参数：data = 账号ID\n" +
            "响应参数：status-message = {1010-当前账号下有未完成的工作}")
    public BaseResDto<Status, String> checkAdmin(@RequestBody BaseReqDto<Integer> baseReqDto) {
        Integer adminId = baseReqDto.getData();
        if (adminId == null) return new BaseResDto<>(Status.PARAMETERERROR);
        return bygAdminService.checkAdmin(adminId);
    }

    /**
     * 停用/启用单位登录账号：取反
     *
     * @param baseReqDto
     * @return
     */
    @PostMapping("/upDownAdmin")
    @ApiOperationSort(4)
    @ApiOperation(value = "4 - 停用/启用单位登录账号：取反", notes = "请求参数：data = 账号ID")
    public BaseResDto<Status, String> upDownAdmin(@RequestBody BaseReqDto<Integer> baseReqDto) {
        Integer adminId = baseReqDto.getData();
        if (adminId == null) return new BaseResDto<>(Status.PARAMETERERROR);
        return bygAdminService.upDownAdmin(adminId);
    }

}